﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using Newtonsoft.Json;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 系统配置
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class ConfigController : AdminBaseController
    {
        //
        // GET: /Admin/Config/
        /// <summary>
        /// 系统配置主表逻辑类
        /// </summary>
        private BLL.Config configBLL = new BLL.Config();
        /// <summary>
        /// 系统配置明细表逻辑类
        /// </summary>
        private BLL.ConfigEntry configEntryBLL = new BLL.ConfigEntry();
        /// <summary>
        /// 系统配置首页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_Config", Order = 1)]
        [LogPageActionFilter("系统配置", Order = 2)]
        public ActionResult Index()
        {
            string entityNo = GlobalConstant.ENTITYNO;

            List<Model.Config> configList = configBLL.GetListAllIsUse(entityNo);
            Dictionary<string, List<Models.CommonValueModel>> dicCommonValueList = new Dictionary<string, List<Models.CommonValueModel>>();

            foreach (Model.Config configModel in configList)
            {
                List<Model.ConfigEntry> tmpEntryList = configEntryBLL.GetModelList(entityNo, configModel.FKeyName, configModel.FConfigClass);
                configModel.ConfigEntryList = tmpEntryList;
                List<Models.CommonValueModel> commonValueList = new List<Models.CommonValueModel>();
                foreach (Model.ConfigEntry configEntryModel in tmpEntryList)
                {
                    commonValueList.Add(new Models.CommonValueModel() { Text = configEntryModel.FDocClsCd, Value = configEntryModel.FDocClsCd });
                }

                dicCommonValueList.Add(configModel.FKeyName + "_" + configModel.FConfigClass, commonValueList);
            }
            string dicEntryJson = "";
            if (dicCommonValueList.Count > 0)
            {
                dicEntryJson = JsonConvert.SerializeObject(dicCommonValueList);
            }
            ViewData["dicEntryJson"] = dicEntryJson;

            return View(configList);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save()
        {
            string errorMessage = "";
            ActionResult actionResult = null;
            string entityNo = GlobalConstant.ENTITYNO;
            List<Model.Config> configList = configBLL.GetListAllIsUse(entityNo);
            Dictionary<string, List<Models.CommonValueModel>> dicCommonValueList = new Dictionary<string, List<Models.CommonValueModel>>();
            string hdDicEntryJson = Fetch.Post("hdDicEntryJson");
            if (hdDicEntryJson != "")
            {
                dicCommonValueList = JsonConvert.DeserializeObject<Dictionary<string, List<Models.CommonValueModel>>>(hdDicEntryJson);
            }
            foreach (Model.Config configModel in configList)
            {
                if (configModel.FIsWithDocCls == "Y")
                {
                    configModel.ConfigEntryList = new List<Model.ConfigEntry>();
                    if (dicCommonValueList.Keys.Contains(configModel.FKeyName + "_" + configModel.FConfigClass))
                    {
                        List<Models.CommonValueModel> commonValueList = dicCommonValueList[configModel.FKeyName + "_" + configModel.FConfigClass];
                        foreach (Models.CommonValueModel comonValueModel in commonValueList)
                        {
                            configModel.ConfigEntryList.Add(new Model.ConfigEntry()
                            {
                                FConfigClass = configModel.FConfigClass,
                                FDocClsCd = comonValueModel.Value,
                                FEntityNo = entityNo,
                                FKeyName = configModel.FKeyName
                            });
                        }
                    }
                }
                else
                {
                    configModel.FKeyValue = Fetch.Post(configModel.FKeyName + "_" + configModel.FConfigClass);
                    configModel.ConfigEntryList = null;
                }
            }

            try
            {

                configBLL.UpdateConfig(configList);
                ViewData["RedirectURL"] = Url.Action("Index", "Config");

                actionResult = View("SuccessMessagePage");
            }
            catch(Exception ee)
            {
                errorMessage = "操作异常:" + ee.Message;
            }
            if (actionResult == null)
            {
                ViewData["message"] = errorMessage;
                string dicEntryJson = "";
                if (dicCommonValueList.Count > 0)
                {
                    dicEntryJson = JsonConvert.SerializeObject(dicCommonValueList);
                }
                ViewData["dicEntryJson"] = dicEntryJson;
                actionResult = View("Index", configList);
            }
            return actionResult;
        }
    }
}
